package com.example.httptest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.renderscript.ScriptGroup;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
    public static final int CHANGE_UI = 1;
    public static final int ERROR = 2;
    HttpURLConnection conn;
    public EditText et_url;
    public Button btn;
    public ImageView iv_pic;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == CHANGE_UI){
                Bitmap bitmap = (Bitmap)msg.obj;
                iv_pic.setImageBitmap(bitmap);
            }else if(msg.what == ERROR){
                Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        获取网址
        et_url = findViewById(R.id.url_et);
//        获取按钮
        btn = findViewById(R.id.btn);
//        获取imageview
        iv_pic = findViewById(R.id.iv_pic);

    }

    public void click(View view){
        final String path = et_url.getText().toString().trim(); //将网址转化为字符串 去掉空格
        if(TextUtils.isEmpty(path)){ //判断输入框是否输入为空
            Toast.makeText(this,"输入不能为空",Toast.LENGTH_LONG).show();
        }else{

            //联网操作比较耗时 要在子线程中进行
            new Thread(){ //新建子线程
                @Override
                public void run() {
                    super.run();
                    try {
                        URL url = new URL(path);
                        //根据URL获取http请求
                        conn = (HttpURLConnection) url.openConnection();
                        //设置请求方式
                        conn.setRequestMethod("GET");
//                        设置超时时间
                        conn.setConnectTimeout(5000);
                        //得到服务器端的响应码
                        int code = conn.getResponseCode();
                        //如果响应码为200则网络访问成功
                        if(code == 200){
                            //获取输入流
                            InputStream is= conn.getInputStream();
                            //将输入流转换为Bitmap对象
                            Bitmap bitmap = BitmapFactory.decodeStream(is);

                            Message msg = new Message();
                            msg.what = CHANGE_UI;
                            msg.obj = bitmap;
                            handler.sendMessage(msg);

                        }
                    } catch (Exception e) {
                        Message msg = new Message();
                        msg.what = ERROR;
                        handler.sendMessage(msg);
                        e.printStackTrace();
                    }
                    conn.disconnect(); //断开网络连接
                }
            }.start();

        }
    }
}
